package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.adapter.MoreAdapter
import com.example.xinhuazidian.adapter.MoreAdapter.OnRecyclerItemClickListener
import com.example.xinhuazidian.bean.More
import com.example.xinhuazidian.databinding.ActivityMoreBinding
import com.example.xinhuazidian.utils.JsonFileUtil
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gyf.immersionbar.ImmersionBar
import com.yechaoa.yutilskt.YUtils
import java.lang.reflect.Type

/**
 * 更多页面
 */
class MoreActivity : AppCompatActivity(), OnRecyclerItemClickListener {

    private lateinit var binding: ActivityMoreBinding
    private lateinit var mapBeanList: MutableList<More>
    private var moreAdapter: MoreAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMoreBinding.inflate(layoutInflater)
        setContentView(binding.root)

        load()
        initView()
        initData()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()
    }

    private fun initData() {
        moreAdapter?.setRecyclerItemClickListener(this)
    }

    @SuppressLint("SetTextI18n")
    private fun load() {
        val str = JsonFileUtil.loadFile(this, "more.json")

        val mapBeanListType: Type = object : TypeToken<ArrayList<More?>?>() {}.type
        mapBeanList = Gson().fromJson(str, mapBeanListType)

        initAdapter(mapBeanList)
    }

    private fun initAdapter(list: MutableList<More>) {
        //设置布局排列方式
        val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL)
        binding.rv.layoutManager = staggeredGridLayoutManager

        //加载适配器
        moreAdapter = MoreAdapter(this, list)
        binding.rv.adapter = moreAdapter
    }

    override fun onRecyclerItemClick(more: More) {
        when (more.id) {
            1 -> {
                //三字经-传统版
                YUtils.showLoading(this, "加载中...")
                intent = Intent(this, SZJActivity::class.java)
                intent.putExtra("fileName", "sanzijing_ct.json")
                startActivity(intent)
            }
            2 -> {
                //三字经-新版
                YUtils.showLoading(this, "加载中...")
                intent = Intent(this, SZJActivity::class.java)
                intent.putExtra("fileName", "sanzijing_xb.json")
                startActivity(intent)
            }
            3 -> {
                //百家姓
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, BJXActivity::class.java))
            }
            4 -> {
                //千字文
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, QianziwenActivity::class.java))
            }
            5 -> {
                //弟子规
                YUtils.showLoading(this, "加载中...")
                intent = Intent(this, DiziguiActivity::class.java)
                intent.putExtra("fileName", "dizigui.json")
                startActivity(intent)
            }
            6 -> {
                //成语
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, CYActivity::class.java))
            }
            7 -> {
                //歇后语
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, XHYActivity::class.java))
            }
            8 -> {
                //唐诗三百首
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, TangshisanbaiActivity::class.java))
            }
            9 -> {
                //水墨唐诗
                YUtils.showLoading(this, "加载中...")
                intent = Intent(this, ShuimotangshiActivity::class.java)
                intent.putExtra("fileName", "shuimotangshi.json")
                startActivity(intent)
            }
            10 -> {
                //千家诗
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, QianjiashiActivity::class.java))
            }
            11 -> {
                //论语
                YUtils.showLoading(this, "加载中...")
                intent = Intent(this, DiziguiActivity::class.java)
                intent.putExtra("fileName", "lunyu.json")
                startActivity(intent)
            }
            12 -> {
                //纳兰性德诗集
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, NalanxingdeshijiActivity::class.java))
            }
            13 -> {
                //曹操诗集
                YUtils.showLoading(this, "加载中...")
                intent = Intent(this, ShuimotangshiActivity::class.java)
                intent.putExtra("fileName", "caocao.json")
                startActivity(intent)
            }
            14 -> {
                //朱子家训
                YUtils.showLoading(this, "加载中...")
                intent = Intent(this, SZJActivity::class.java)
                intent.putExtra("fileName", "zhuzijiaxun.json")
                startActivity(intent)
            }
            15 -> {
                //诗经
                YUtils.showLoading(this, "加载中...")
                startActivity(Intent(this, ShijingActivity::class.java))
            }
        }
    }
}